<template>
  <div>
    <el-button type="primary" @click="handleAdd">Add</el-button>
    <el-table :data="tableData" style="width: 100%">
      <!--your columns goes here-->
      <el-table-column prop="custom_name" label="客户名称" width="180"></el-table-column>
      <el-table-column prop="access_key" label="密钥" width="180"></el-table-column>
      <el-table-column label="Operations">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" title="Edit" @close="resetForm">
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="Date" prop="date">
          <el-date-picker v-model="form.date" type="date" placeholder="Pick a Date"></el-date-picker>
        </el-form-item>
        <el-form-item label="Name" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="submitForm('form')">Confirm</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAPI } from '@/api/custom'
export default {
  data() {
    return {
      tableData: [], // data from your API
      dialogVisible: false,
      form: {},
      rules: {
        date: [{ type: 'date', required: true, message: 'Please pick a date', trigger: 'change' }],
        name: [{ required: true, message: 'Please input name', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async fetchData() {
      // request your API data
      const { data } = await listAPI()
      this.tableData = data
    },
    handleAdd() {
      this.resetForm()
      this.dialogVisible = true
    },
    handleEdit(index, row) {
      this.form = Object.assign({}, row) // copy row data to form
      this.dialogVisible = true
    },
    handleDelete(index, row) {
      // request delete API
      // on success do: this.tableData.splice(index, 1);
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // request add or update API
          // on success do: this.fetchData();
        } else {
          return false
        }
      })
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>
